﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
 

public partial class Frame_User : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //string strPageIndex = Request["PageIndex"] != null ? Request["PageIndex"].ToString() : "";

            //this.GetCode();

            //if (!string.IsNullOrEmpty(strPageIndex))
            //{
            //    this.hidPageIndex.Value = strPageIndex;
            //}

            //this.txtUserid.Text = Session["UserQueryUserid"] != null ? Session["UserQueryUserid"].ToString() : "";
            //this.txtCusabbr.Text = Session["UserQueryCusabbr"] != null ? Session["UserQueryCusabbr"].ToString() : "";

            //if (Session["UserQueryRoleref"] != null && !"0".Equals(Session["UserQueryRoleref"].ToString()))
            //{
            //    ddlRole.SelectedValue = Session["UserQueryRoleref"].ToString();
            //}

            //SystemOption sysOption = new SystemOption();
            //hidPageCount.Value = sysOption.GetPageSize().ToString();

            //this.GetData();

        }
    }

    /// <summary>
    /// 获取代码
    /// </summary>
    protected void GetCode()
    {
        //RolePurviewBL bl = new RolePurviewBL();

        //List<RolePurviewBL.RoleDisplay> query = bl.GetRoleInfoByYard(null);
        //this.ddlRole.DataSource = query;
        //this.ddlRole.DataTextField = "ROLENM";
        //this.ddlRole.DataValueField = "ROLEREF";
        //this.ddlRole.DataBind();

        //ListItem item = new ListItem("请选择", "0");
        //this.ddlRole.Items.Insert(0, item);
        //this.ddlRole.SelectedIndex = 0;
    }

    /// <summary>
    /// 获取数据
    /// </summary>
    private void GetData()
    {
        //UserBL bl = new UserBL();

        //string roleref = "";
        //if (Session["UserQueryRoleref"] != null && !"0".Equals(Session["UserQueryRoleref"].ToString()))
        //{
        //    roleref = Session["UserQueryRoleref"].ToString();
        //}

        //string UserName = Session["UserQueryUserid"] != null ? Session["UserQueryUserid"].ToString() : "";
        //string cusabbr = Session["UserQueryCusabbr"] != null ? Session["UserQueryCusabbr"].ToString() : "";

        //List<UserTable> query = bl.GetUserInfo(UserName, cusabbr, roleref);
        //this.grdUser.DataSource = query;

        //grdUser.PageSize = int.Parse(hidPageCount.Value);
        //grdUser.PageIndex = int.Parse(this.hidPageIndex.Value);
        //grdUser.DataBind();

        
        //if (query == null)
        //{
        //    lblDataCount.Text = "共有0条记录。";
        //}
        //else
        //{
        //    int count = query.Count;
        //    lblDataCount.Text = "共有" + count.ToString() + "条记录。";
        //}
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnFind_Click(object sender, EventArgs e)
    {
        Session["UserQueryRoleref"] = this.ddlRole.SelectedValue.Trim();
        Session["UserQueryUserid"] = this.txtUserid.Text.Trim();
        Session["UserQueryCusabbr"] = this.txtCusabbr.Text.Trim();
        this.GetData();
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grdUser.PageIndex = e.NewPageIndex;
        hidPageIndex.Value = e.NewPageIndex.ToString();
        this.GetData();
    }
    
    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserDetail.aspx?PageIndex=" + int.Parse(hidPageIndex.Value));
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        LinkButton linkBtn = sender as LinkButton;
        Response.Redirect("UserDetail.aspx?userid=" + linkBtn.CommandArgument.ToString() + "&PageIndex=" + int.Parse(hidPageIndex.Value));
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        //LinkButton linkBtn = sender as LinkButton;
        //string userid = linkBtn.CommandArgument.ToString();
        //try
        //{
        //    UserBL bl = new UserBL();

        //    if (bl.DeleteUser(userid))
        //    {
        //        lblMessage.ForeColor = System.Drawing.Color.Green;
        //        lblMessage.Text = " 删除成功。";
        //    }

        //    else
        //    {
        //        lblMessage.Text = "保存失败。";
        //    }

        //    this.GetData();
        //}
        //catch
        //{
        //    this.lblMessage.Text = "连接数据库失败。";

        //}
    }

    /// <summary>
    /// 角色设置
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnRole_Click(object sender, EventArgs e)
    {
        LinkButton linkBtn = sender as LinkButton;
        Response.Redirect("UserRole.aspx?userid=" + linkBtn.CommandArgument.ToString() + "&PageIndex=" + int.Parse(hidPageIndex.Value));

    }

    /// <summary>
    /// 刷新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        this.hidPageIndex.Value = "0";

        Session["UserQueryRoleref"] = null;
        Session["UserQueryUserid"] = null;
        Session["UserQueryCusabbr"] = null;

        ddlRole.SelectedValue = "0";
        txtUserid.Text = "";
        txtCusabbr.Text = "";

        this.GetData();
    }

    /// <summary>
    /// 重置密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnResetPassword_Click(object sender, EventArgs e)
    {
        //LinkButton linkBtn = sender as LinkButton;
        //string userid = linkBtn.CommandArgument.ToString();

        //UserBL bl = new UserBL();
        //Model.Web.UserTable usertb = bl.GetUserInfo(userid);
        //usertb.Password = "000000";
        //bl.UpdateUser(usertb);
        ////Response.Redirect("UserRole.aspx?userid=" + linkBtn.CommandArgument.ToString()
    }
}